package com.lbz.controller.backend;

import com.lbz.common.Constant;
import com.lbz.common.ResponseCode;
import com.lbz.common.ServerResponse;
import com.lbz.entity.User;
import com.lbz.service.CategoryService;
import com.lbz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author 林必昭
 * @Date 2019/6/23 15:04
 */

@Controller
@RequestMapping("/manage/category/")
public class CategoryManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;

    /**
     *  新增品类
     * @param session
     * @param categoryName
     * @param parentId 当父类id=0时说明是根节点
     * @return
     */
    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
         User user = (User) session.getAttribute(Constant.CURRENT_USER);
         if (user==null){
             return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
         }
         //校验是否是管理员
        ServerResponse serverResponse = userService.checkRoleAdmin(user);
         if (serverResponse.isSuccess()){
             //如果是管理员，添加新增分类逻辑
              return categoryService.addCategory(categoryName, parentId);

         }else {
             return ServerResponse.createByErrorMessage("无权限操作，请以管理员身份登陆");
         }
    }

    /**
     * 删除品类
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping("del_category.do")
    @ResponseBody
    public ServerResponse deleteCategory(HttpSession session, Integer categoryId){
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是否是管理员
        ServerResponse serverResponse = userService.checkRoleAdmin(user);
        if (serverResponse.isSuccess()){
            //如果是管理员，添加新增分类逻辑
            return categoryService.deleteCategory(categoryId);

        }else {
            return ServerResponse.createByErrorMessage("无权限操作，请以管理员身份登陆");
        }
    }

    /**
     * 更新品类名称
     * @param session
     * @param categoryId
     * @param categoryName
     * @return
     */
    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是否是管理员
        ServerResponse serverResponse = userService.checkRoleAdmin(user);
        if (serverResponse.isSuccess()){
            //如果是管理员，添加更新分类逻辑
            return categoryService.updateCategoryName(categoryId,categoryName);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作，请以管理员身份登陆");
        }
    }

    /**
     * 获取品类子节点(平级)
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session,
                                                      @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是否是管理员
        ServerResponse serverResponse = userService.checkRoleAdmin(user);
        if (serverResponse.isSuccess()){
            //如果是管理员，添加查询品类子节点(平级)逻辑，不递归，保持平级
            return categoryService.selectChildrenParallelCategory(categoryId);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作，请以管理员身份登陆");
        }
    }

    /**
     * 深度查询，获取当前分类id及递归子节点分类id
     * 返回本身来它面的子节点
     * 比如：http://localhost:8080/manage/category/get_deep_category.do?categoryId=100001
     *
     * 返回：
     *         100001,
     *         100006,
     *         100007,
     *         100010,
     *         100009,
     *         100008
     *
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是否是管理员
        ServerResponse serverResponse = userService.checkRoleAdmin(user);
        if (serverResponse.isSuccess()){
            //如果是管理员，查询当前节点的id以及递归子节点的分类id
            //假设0->10->100->1000，0的下一级子孩子节点为10，10的下一级节点为100，100的下一级节点为1000
            return categoryService.selectCategoryAndChildrenById(categoryId);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作，请以管理员身份登陆");
        }
    }
}
